import 'dart:convert';

import 'ChannelUtils.dart';

@Deprecated("弃用")
const SET_MUTE_DURATION_SECONDS = 'setMuteDurationSeconds';
@Deprecated("弃用")
const SET_DISPLAY_NOTIFICATION_NUMBER = 'setDisplayNotificationNumber';
const OPEN_LOCATION_SERVICE_SETTING = 'openLocationServiceSetting';
const OPEN_NOTIFICATION_SETTINGS = 'openNotificationSettings';
const INSTALL_APK = 'installApk';

class CommonChannel {
  static CommonChannel? _channel;

  static CommonChannel? getInstance() {
    if (_channel == null) {
      _channel = new CommonChannel();
    }
    return _channel;
  }

  /// 友盟 设置显示通知冷却时间 默认情况下，统一设备在1分钟内收到同一个应用的多条通知时，不会重复提醒
  @Deprecated("弃用")
  Future<bool> setMuteDurationSeconds(int secodes) async {
    Map params = {
      "code": 100, "message": "设置显示通知冷却时间", "content": secodes
    };
    String result = await ChannelUtils().toolsChannelMethodWithParams(SET_MUTE_DURATION_SECONDS, params);
    if (jsonDecode(result)['code'] == "200") {
      print("设置显示通知冷却时间 true");
      return jsonDecode(result)['content'];
    }
    print("设置显示通知冷却时间 false");
    return false;
  }

  /// 友盟 设置显示通知的数量  参数为0 表示不限制个数
  @Deprecated("弃用")
  Future<bool> setDisplayNotificationNumber(int number) async {
    Map params = {
      "code": 100, "message": "设置显示通知的数量", "content": number
    };
    String result = await ChannelUtils().toolsChannelMethodWithParams(SET_DISPLAY_NOTIFICATION_NUMBER, params);
    if (jsonDecode(result)['code'] == "200") {
      print("设置显示通知的数量 true");
      return jsonDecode(result)['content'];
    }
    print("设置显示通知的数量 false");
    return false;
  }

  /// 打开定位服务设置
  Future<bool> openLocationServiceSetting() async{
    Map params = {
      "code": 100, "message": "打开定位服务设置", "content": ""
    };
    String result = await ChannelUtils().toolsChannelMethodWithParams(OPEN_LOCATION_SERVICE_SETTING, params);

    if (jsonDecode(result)['code'] == "200") {
      return jsonDecode(result)['content'];
    }
    return false;
  }

  /// 打开通知服务设置
  Future<void> openNotificationSettings() async {
    await ChannelUtils().toolsChannelMethodWithParams(OPEN_NOTIFICATION_SETTINGS);
  }

  /// 安装apk文件
  Future<bool> installApk(String path) async {
    Map params = {
      "code": 100, "message": "安装apk文件", "content": path
    };
    String result = await ChannelUtils().toolsChannelMethodWithParams(INSTALL_APK, params);
    if (jsonDecode(result)['code'] == "200") {
      return jsonDecode(result)['content'];
    }
    return false;
  }
}
